﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FindIt.DAL;
using MySql.Data.MySqlClient;

namespace FindIt.BLL
{
    public class Image
    {
        private static double creditChangeForGoodVote = double.Parse(ConfigurationManager.AppSettings["creditChangeForGoodVote"]);
        public enum StatusType { normal, warned, removed };
        private string _title;
        private string _url;
        private int _size;
        private bool _isRestricted;
        private DateTime _uploadDate;
        private DateTime _lastUpdate;
        private int _numViews;
        private DateTime _lastViewed;
        private StatusType _status;
        private HashSet<string> _keywords;
        private ArrayList _complaints;
        private string _owner;


        #region Properties
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }

        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public bool IsRestricted
        {
            get { return _isRestricted; }
            set { _isRestricted = value; }
        }

        public DateTime UploadDate
        {
            get { return _uploadDate; }
            set { _uploadDate = value; }
        }

        public DateTime LastUpdate
        {
            get { return _lastUpdate; }
            set { _lastUpdate = value; }
        }

        public int NumViews
        {
            get { return _numViews; }
            set { _numViews = value; }
        }

        public DateTime LastViewed
        {
            get { return _lastViewed; }
            set { _lastViewed = value; }
        }

        public StatusType Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public HashSet<string> KeyWords
        {
            get { return _keywords; }
            set { _keywords = value; }
        }

        public ArrayList Complaints
        {
            get { return _complaints; }
            set { _complaints = value; }
        }

        public string Owner
        {
            get { return _owner; }
            set { _owner = value; }
        }
        #endregion

        #region Constructors
        public Image()
        {
        }

        public Image(string title, string url, int size, bool isRestricted, DateTime uploadDate, DateTime lastUpdate, int numViews, DateTime lastViewed, StatusType status, string owner)
        {
            this._title = title;
            this._url = url;
            this._size = size;
            this._isRestricted = isRestricted;
            this._uploadDate = uploadDate;
            this._lastUpdate = lastUpdate;
            this._numViews = numViews;
            this._lastViewed = lastViewed;
            this._status = status;
            this._owner = owner;
        }

        public Image(string title, string url, int size, bool isRestricted, DateTime uploadDate, DateTime lastUpdate, int numViews, DateTime lastViewed, StatusType status, string owner, HashSet<string> keywords)
        {
            this._title = title;
            this._url = url;
            this._size = size;
            this._isRestricted = isRestricted;
            this._uploadDate = uploadDate;
            this._lastUpdate = lastUpdate;
            this._numViews = numViews;
            this._lastViewed = lastViewed;
            this._status = status;
            this._owner = owner;
            this._keywords = keywords;
        }

        public Image(string title, string url, int size, bool isRestricted, DateTime uploadDate, DateTime lastUpdate, int numViews, DateTime lastViewed, StatusType status, string owner, HashSet<string> keywords, ArrayList complaints)
        {
            this._title = title;
            this._url = url;
            this._size = size;
            this._isRestricted = isRestricted;
            this._uploadDate = uploadDate;
            this._lastUpdate = lastUpdate;
            this._numViews = numViews;
            this._lastViewed = lastViewed;
            this._status = status;
            this._owner = owner;
            this._keywords = keywords;
            this._complaints = complaints;
        }
        #endregion

        public void AddImageToDB()
        {
            ImageDB db = new ImageDB();
            db.AddImage(this);
            AddKeywords(this._url, this._keywords);
        }

        public static Image GetImageByURL(string url)
        {
            Image image = new Image();
            ImageDB db = new ImageDB();
            MySqlDataReader dr = db.GetImageByURL(url);

            while (dr.Read())
            {
                image._title = dr["Title"].ToString();
                image._url = dr["URL"].ToString();
                image._size = int.Parse(dr["Size"].ToString());
                image._isRestricted = bool.Parse(dr["IsRestricted"].ToString());
                image._uploadDate = DateTime.Parse(dr["UploadDate"].ToString());
                image._lastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                image._numViews = int.Parse(dr["NumViews"].ToString());
                image._lastViewed = DateTime.Parse(dr["LastViewed"].ToString());
                image._status = (StatusType)Enum.Parse(typeof(StatusType), dr["Status"].ToString());
                image._owner = dr["Owner"].ToString();
            }

            return image;
        }

        public static void AddKeyword(string url, string key)
        {
            ImageDB db = new ImageDB();
            db.AddKeyword(url, key);
        }


        public void AddKeywords(string url, HashSet<string> keys)
        {
            ImageDB db = new ImageDB();
            db.AddKeywords(url, keys);
        }

        public static void AddTentativeKeyword(string url, string key, string voter)
        {
            ImageDB db = new ImageDB();
            db.AddTentativeKeyword(url, key, voter);
        }

        public void SubmitComplaint(string complaint)
        {
        }

        public static ArrayList GetImageList()
        {
            ArrayList list = new ArrayList();
            return list;
        }

        public static ArrayList GetPendingComplaints()
        {
            ArrayList list = new ArrayList();
            return list;
        }

        public static string GetUnknownCaptchaImage()
        {
            ImageDB db = new ImageDB();
            string unknownImageURL = db.GetUnknownCaptchaImage();
            return unknownImageURL;
        }

        public static string GetKnownCaptchaImage()
        {
            ImageDB db = new ImageDB();
            string knownImageURL = db.GetKnownCaptchaImage();
            return knownImageURL;
        }

        public static void SaveTentativeKeyword(string url, string key, string voter)
        {
            if (FindIt.BLL.Image.CheckIfPermanentKeyword(url, key))
            {
            }

            else if (GetNumberOfVotesForTentativeKeyword(url, key) >= 0 && !CheckIfVoterForTentativeKeyword(url, key, voter))
            {
                if (GetNumberOfVotesForTentativeKeyword(url, key) < 2)
                {
                    AddTentativeKeyword(url, key, voter);
                }

                else if (GetNumberOfVotesForTentativeKeyword(url, key) == 2)
                {
                    AddTentativeKeyword(url, key, voter);
                    IncreaseVoterCredit(url, key, creditChangeForGoodVote);
                    RemoveTentativeKeyword(url, key);
                    AddKeyword(url, key);
                    if (GetNumberOfPermanentKeywords(url) >= 10)
                    {
                        RemoveAllTentativeKeywords(url);
                    }
                }
            }
        }

        public static bool CheckIfPermanentKeyword(string url, string key)
        {
            ImageDB db = new ImageDB();
            return db.CheckIfPermanentKeyword(url, key);
        }

        public static int GetNumberOfVotesForTentativeKeyword(string url, string key)
        {
            ImageDB db = new ImageDB();
            return db.GetNumberOfVotesForTentativeKeyword(url, key);
        }

        public static bool CheckIfVoterForTentativeKeyword(string url, string key, string voter)
        {
            ImageDB db = new ImageDB();
            return db.CheckIfVoterForTentativeKeyword(url, key, voter);
        }

        public static void IncreaseVoterCredit(string url, string key, double difference)
        {
            ImageDB db = new ImageDB();
            MySqlDataReader dr = db.GetVoters(url, key);

            while (dr.Read())
            {
                PrivilegedUser.ChangeCredit(dr["Voter"].ToString(), difference);
            }
        }

        public static void RemoveTentativeKeyword(string url, string key)
        {
            ImageDB db = new ImageDB();
            db.RemoveTentativeKeyword(url, key);
        }

        public static void RemoveAllTentativeKeywords(string url)
        {
            ImageDB db = new ImageDB();
            db.RemoveAllTentativeKeywords(url);
        }

        public static int GetNumberOfPermanentKeywords(string url)
        {
            ImageDB db = new ImageDB();
            return db.GetNumberOfPermanentKeywords(url);
        }
    }
}
